<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Commission;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
    	$this->auth->direct(13);
	    $userInfo = $this->auth->getUserinfo();
        $this->success('请求成功',$userInfo);
    }
    
    public function notice() {
    	$rows = Db::name('commission')->alias('c')
		    ->join('user u', 'c.user_id = u.id', 'left')
		    ->where(['c.status' => Commission::STATUS_Y])
		    ->order(['c.create_time' => 'desc'])
		    ->field('u.nickname, c.amount')
		    ->limit(50)
		    ->select();
    	
    	$number = count($rows);
    	if($number < 50) {
    		for ($i=0; $i < 50-$number; $i++){
    			$rows[] = [
    			    'nickname' => '1'.mt_rand(30, 99).'****'.mt_rand(001, 999),
				    'amount' => mt_rand(30, 500)
			    ];
		    }
	    }
    	
    	$this->success('查询成功', $rows);
    }
	
	/**
	 * 客服
	 */
    public function service(){
    	$row = Db::name('service')->find();
    	$this->success('查询成功', $row);
    }
    
    
    /**
     * 分享排行
     */
    public function shareRanking() {
        $rows = Db::name('shareRecord')->alias('r')
	        ->join('user u', 'r.user_id = u.id', 'left')
	        ->group('r.user_id')
	        ->order(['num' => 'desc'])
	        ->field('u.nickname, u.avatar, count(r.id) as num, r.user_id', 'r.user_id')
	        ->select();
        
        $userId = $this->auth->id;
        
        $my = null;
        
        foreach ($rows as $index => &$row) {
        	$row['num'] = unit_number($row['num']);
            if($row['user_id'] == $userId) {
            	$my =  [
            		'index' => $index + 1,
		            'number' => $row['num']
	            ];
            }
        }
        
        $this->success('查询成功', [
        	'my' => $my,
	        'list' => $rows
        ]);
    }
	
	
	/**
	 * 分享排行
	 */
	public function commissionRanking() {
		$rows = Db::name('commission')->alias('r')
			->join('user u', 'r.user_id = u.id', 'left')
			->where(['r.status' => Commission::STATUS_Y])
			->group('r.user_id')
			->order(['amount' => 'desc'])
			->field('u.nickname, u.avatar, sum(r.amount) as amount, r.user_id', 'r.user_id')
			->select();
		
		$userId = $this->auth->id;
		
		$my = null;
		
		$maxAmount = 200;
		
		foreach ($rows as $index => &$row) {
			$maxAmount = $row['amount'];
			$row['amount'] = unit_number($row['amount']);
			if($row['user_id'] == $userId) {
				$my =  [
					'index' => $index + 1,
					'amount' => $row['amount']
				];
			}
		}
		
		$count = count($rows);
		if($count < 20) {
			for($i = 0; $i<20-$count; $i++) {
				$min = $maxAmount/20*100;
				$lessAmount = mt_rand($min/10, $min)/100;
				$amount = $maxAmount-$lessAmount;
				$maxAmount = $amount;
				$rows[]  = [
					'avatar' => '',
					'nickname' => mt_rand(130, 199).'****'.mt_rand(1000, 9999),
					'amount' => unit_number(round($amount, 2))
				];
			}
		}
		
		$this->success('查询成功', [
			'my' => $my,
			'list' => $rows
		]);
	}
	
	/**
	 * 门店
	 */
	public function store() {
		$rows = Db::name('store')->field('id, name, address,lat, lng')
			->select();
		$this->success('查询成功', $rows);
	}
	
	
	public function paySuccess($id) {
		$model = new \app\common\model\Order();
		$orderRes = $model->paySuccess($id);
		dump($orderRes);
	}
	
	/**
	 * 优惠券
	 */
	public function coupon() {
		
		$row = Db::name('coupon')->where(['status' => 1])->field('id, amount, name')->find();
		$user_id = '';
		$userId = $this->auth->id;
		if($row) {
			if($userId) {
				$coupon = Db::name('userCoupon')->where(['coupon_id' => $row['id'], 'user_id' => $userId])->find();
				$user_id = $coupon ? $userId : '';
			}
			
			$row['user_id'] = $user_id;
		}
		
		$this->success('查询成功', $row);
	}
	
	/**
	 * 设置
	 */
	public function setting() {
		$row = Db::name('setting')->find();
		$this->success('查询成功', $row);
	}
}
